﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using LibraryGuard.Interfaces.Configuration;

namespace LibraryGuard.Configuration
{
	/// <summary>
	/// Root configuration.
	/// </summary>
	public sealed class Configuration
		: IConfiguration
	{
		#region Internal fields
		internal readonly List<DirectoryInfo> DirectoryInfos = new List<DirectoryInfo>();
		#endregion

		#region Xml config
		public List<string> Directories { get; set; }
		public AdvancedConfiguration Advanced { get; set; }

		[XmlAttribute("DueTime")]
		public string XmlDueTime
		{
			get { return this.DueTime.ToString(); }
			set { this.DueTime = TimeSpan.Parse(value); }
		}

		[XmlAttribute("WorkingTime")]
		public string XmlWorkingTime
		{
			get { return this.WorkingTime.ToString(); }
			set { this.WorkingTime = TimeSpan.Parse(value); }
		}
		#endregion

		#region IConfiguration Members
		/// <inheritdoc />
		[XmlIgnore]
		List<DirectoryInfo> IConfiguration.Directories
		{
			get { return this.DirectoryInfos; }
		}

		/// <inheritdoc />
		public List<string> Extensions { get; set; }

		/// <inheritdoc />
		[XmlAttribute]
		public bool Continuous { get; set; }

		/// <inheritdoc />
		[XmlAttribute]
		public Mode Mode { get; set; }

		/// <inheritdoc />
		[XmlIgnore]
		IAdvancedConfiguration IConfiguration.Advanced
		{
			get { return this.Advanced; }
		}

		/// <inheritdoc />
		[XmlIgnore]
		public TimeSpan DueTime { get; set; }

		/// <inheritdoc />
		[XmlIgnore]
		public TimeSpan WorkingTime { get; set; }
		#endregion

		#region Guard Configurations
		/// <summary>
		/// Path configuration.
		/// </summary>
		[Guard(typeof(Guards.PathGuard))]
		public PathGuard Path { get; set; }

		/// <summary>
		/// Empty directories guard.
		/// </summary>
		[Guard(typeof(Guards.EmptyDirectoriesGuard))]
		public EmptyDirectoriesGuard EmptyDirectories { get; set; }

		/// <summary>
		/// Playlist guard.
		/// </summary>
		[Guard(typeof(Guards.PlaylistGuard))]
		public PlaylistGuard Playlist { get; set; }

		/// <summary>
		/// Unwanted files guard.
		/// </summary>
		[Guard(typeof(Guards.UnwantedFilesGuard))]
		public UnwantedFilesGuard UnwantedFiles { get; set; }

		/// <summary>
		/// Tags guard.
		/// </summary>
		[Guard(typeof(Guards.TagsGuard))]
		public TagsGuard Tags { get; set; }
		#endregion

		#region Constructor
		public Configuration()
		{
			this.Directories = new List<string>();
			this.Extensions = new List<string>();
			this.Advanced = new AdvancedConfiguration();

			this.DueTime = TimeSpan.Zero;

			this.Path = new PathGuard();
			this.EmptyDirectories = new EmptyDirectoriesGuard();
			this.Playlist = new PlaylistGuard();
			this.UnwantedFiles = new UnwantedFilesGuard();
			this.Tags = new TagsGuard();
		}
		#endregion
	}
}
